package com.android.zgx.calllog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.TextView;

public class Main extends Activity {
	/*
	 * Variable Definition
	 */
	private TextView mResultText;
	private List<Map<String, Object>> mResultList;

	final String PHONE_NUMBER = "PhoneNumber";
	final String CONNECTION_TIMES = "ConnectionTimes";
	final String CONNECTION_LONG = "ConnectionLong";
	final String AVE_CONNECT_LONG = "AveConnectLong";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
		readCallLog(mResultList);
		showCallLog(mResultList);
	}

	// Initialize Control and Variable
	private void init() {
		mResultText = (TextView) findViewById(R.id.resultText);

		mResultList = new ArrayList<Map<String, Object>>();
	}

	/*
	 * Android Call Log
	 * 
	 * @Param ResultList for in/out
	 */
	private void readCallLog(List<Map<String, Object>> ResultList) {
		// Variable Definition
		String phoneNumber = "";
		Integer durationSeconds;

		// Access to the Content
		ContentResolver cr = getContentResolver();

		// Query for Call Log
		final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, new String[] {
				CallLog.Calls.NUMBER, CallLog.Calls.DURATION }, null, null,
				"number desc");

		// Work with Records
		for (int i = 0; i < cursor.getCount(); i++) {
			Map<String, Object> map = new HashMap<String, Object>();

			cursor.moveToPosition(i);
			phoneNumber = cursor.getString(0);
			map.put(PHONE_NUMBER, phoneNumber);
			map.put(CONNECTION_TIMES, 1);
			durationSeconds = cursor.getInt(1);
			map.put(CONNECTION_LONG, durationSeconds);
			map.put(AVE_CONNECT_LONG, durationSeconds);

			// if the PhoneNumber exists in the List
			for (Map<String, Object> listItem : ResultList) {
				if (listItem.get(PHONE_NUMBER).toString().equals(phoneNumber)) {
					int times = 1 + Integer.parseInt(listItem.get(
							CONNECTION_TIMES).toString());
					map.put(CONNECTION_TIMES, times);
					Integer seconds = durationSeconds
							+ Integer.parseInt(listItem.get(CONNECTION_LONG)
									.toString());
					map.put(CONNECTION_LONG, seconds);
					map.put(AVE_CONNECT_LONG, seconds / times);
					ResultList.remove(listItem);
					break;
				}
			}
			ResultList.add(0,map);
		}
	}

	/*
	 * Format Result and Show in the TextView
	 * 
	 * @Param ResultList for in
	 */
	private void showCallLog(List<Map<String, Object>> ResultList) {
		// if the PhoneNumber exists in the List
		String tmpStr = "";
		for (Map<String, Object> listItem : ResultList) {
			tmpStr += listItem.get(PHONE_NUMBER).toString() + ",";
			tmpStr += listItem.get(CONNECTION_TIMES).toString() + ",";
			tmpStr += listItem.get(CONNECTION_LONG).toString() + ",";
			tmpStr += listItem.get(AVE_CONNECT_LONG).toString() + "\r\n";
		}
		mResultText.setText(tmpStr);
	}
}
